﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace rambla.raws.rats
{
    public class Jobs
    {
        private ratsConnection conn;

        internal Jobs(ratsConnection conn)
        {
            this.conn = conn;
        }

        public Job[] items
        {
            get
            {
                return conn.getObjects<Job>("/job/");
            }
        }

        public void deleteItem(Job item)
        {
            conn.deleteObject("/job/" + item.id + "/");
        }

        public void createItem(Job item)
        {
            conn.postObject<Job>("/job", item);
        }

        public void updateItem(Job item)
        {
            conn.postObject<Job>("/job/" + item.id + "/", item);
        }

        public Job getItem(string id)
        {
            return conn.getObject<Job>("/job/" + id + "/");
        }
    }

    public enum jobStatus
    {
        REQUEST_RECEIVED = 1,               // This state is returned by RATS immediately after receiving the request.
        IMPORT_IN_PROGRESS = 2,             // RATS has begun importing the source file.
        IMPORT_SUCCEEDED = 3,               // RATS has successfully imported the source file.
        TRANSCODING_IN_PROGRESS = 4,        // RATS has begun transcoding the source file.
        TRANSCODING_SUCCEEDED = 5,          // RATS has successfully transcoded the source file.
        EXPORT_IN_PROGRESS = 6,             // RATS has begun exporting the source file.
        FINISHED = 7,                       // RATS has successfully exported the source file. This completes the processing cycle.
        IMPORT_FAILED = 8,                  // RATS has failed importing the source file.
        TRANSCODING_FAILED = 9,             // RATS has failed transcoding the source file.
        EXPORT_FAILED = 10                  // RATS has failed exporting the transcoded file.
    }

    [XmlRoot(Namespace = "http://rambla.be/raws/ns-metadata/1.0", ElementName = "params")]
    public class Job
    {
        /// <summary>
        /// unique (numerical) identifier for the job instance, given by the RATS server when creating it
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string id;

        /// <summary>
        /// numeric identifier that describes the job's processing status
        /// </summary>
        [XmlElement(IsNullable = true)]
        public jobStatus status;

        /// <summary>
        /// the ID of an existing input profile
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string input;

        /// <summary>
        /// the ID of an existing output profile
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string output;

        /// <summary>
        /// the ID of an existing transcoding format profile
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string format;

        /// <summary>
        /// Location of the source file that needs to be transcoded. In case of a previously uploaded file, it equals the RATS src filename. In case of a remote file, it can be a filename or a relative path (to be appended to the input profile's base_uri).
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string src_location;

        /// <summary>
        /// Location to which the transcoded file needs to be exported. If the filename does not include an extension (e.g. 'my_file'), RATS will automatically append the correct extension ('my_file.mp4'). If the filename does include an extension, this will be used (even if incorrect). See below for more details.
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string tgt_location;

        /// <summary>
        /// Offset from the beginning of the source file at which the transcoding operation must be started. The value provided can be a number of seconds (= default) or a percentage (= number followed by a % sign, e.g. '20%').
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string startpos;

        /// <summary>
        /// Offset in seconds from the BEGINNING of the source file or from startpos if set, at which the transcoding operation must be ended.
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string endpos;

        /// <summary>
        /// placeholder for data to be sent by the RATS client
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string client_passthru;

        /// <summary>
        ///placeholder for sending additional input data, for custom back-end operations 
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string client_input;

        /// <summary>
        /// the uri at which the transc metadata are accessible
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string transc;

        /// <summary>
        /// contains the uri of the transc media item (on the RATS server) 
        /// </summary>
        [XmlElement(IsNullable = true)]
        public string transc_media;
    }
}
